#define ROR32(x, n) (((x) >> (n)) | (((x) << (32 - (n))) & 0xFFFFFFFF))
#define ROL32(x, n) ((((x) << (n)) & 0xFFFFFFFF) | ((x) >> (32 - (n))))

typedef struct {
    uint32_t s1[256], s2[256], s3[256], s4[256], k[42];
} TwofishContext;

int twofishInit(TwofishContext *context, const uint8_t *key, size_t keyLen);
void twofishDecryptBlock(TwofishContext *context, const uint8_t *input, uint8_t *output);

int mbedtls_md5(const unsigned char *input, size_t ilen, unsigned char output[16]);

#define MBEDTLS_AES_ENCRYPT     1
#define MBEDTLS_AES_DECRYPT     0

typedef struct mbedtls_aes_context {
    int nr;
    size_t rk_offset;
    uint32_t buf[68];
} mbedtls_aes_context;

typedef struct mbedtls_aes_xts_context {
    mbedtls_aes_context crypt;
    mbedtls_aes_context tweak;
} mbedtls_aes_xts_context;

void mbedtls_aes_init(mbedtls_aes_context *ctx);
void mbedtls_aes_free(mbedtls_aes_context *ctx);
void mbedtls_aes_xts_init(mbedtls_aes_xts_context *ctx);
void mbedtls_aes_xts_free(mbedtls_aes_xts_context *ctx);
int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key, unsigned int keybits);
int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key, unsigned int keybits);
int mbedtls_aes_xts_setkey_enc(mbedtls_aes_xts_context *ctx, const unsigned char *key, unsigned int keybits);
int mbedtls_aes_xts_setkey_dec(mbedtls_aes_xts_context *ctx, const unsigned char *key, unsigned int keybits);
int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx, int mode, const unsigned char input[16], unsigned char output[16]);
int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx, int mode, size_t length, unsigned char iv[16],
    const unsigned char *input, unsigned char *output);
int mbedtls_aes_crypt_xts(mbedtls_aes_xts_context *ctx, int mode, size_t length, const unsigned char data_unit[16],
    const unsigned char *input, unsigned char *output);
int mbedtls_aes_crypt_cfb128(mbedtls_aes_context *ctx, int mode, size_t length, size_t *iv_off, unsigned char iv[16],
    const unsigned char *input, unsigned char *output);
int mbedtls_aes_crypt_cfb8(mbedtls_aes_context *ctx, int mode, size_t length, unsigned char iv[16],
    const unsigned char *input, unsigned char *output);
int mbedtls_aes_crypt_ofb(mbedtls_aes_context *ctx, size_t length, size_t *iv_off, unsigned char iv[16],
    const unsigned char *input, unsigned char *output);
int mbedtls_aes_crypt_ctr(mbedtls_aes_context *ctx, size_t length, size_t *nc_off,
    unsigned char nonce_counter[16], unsigned char stream_block[16],
    const unsigned char *input, unsigned char *output);
